using FytSoa.Application.Shop;
using FytSoa.Common.Enum;
using FytSoa.Domain.Car;
using FytSoa.Domain.Core.Result;
using FytSoa.Domain.User;
using FytSoa.Sugar;
using MapsterMapper;
using Microsoft.AspNetCore.Mvc;

namespace FytSoa.Application.App;
[ApiExplorerSettings(GroupName = "v5")]
public class AppGameService:IApplicationService
{
    private readonly IMapper _mapper;
    private readonly ShopPointsLogService _logService;
    private readonly SugarRepository<CarsGame> _thisRepository;
    public AppGameService(IMapper mapper
        ,ShopPointsLogService logService
        ,SugarRepository<CarsGame> thisRepository)
    {
        _mapper = mapper;
        _logService = logService;
        _thisRepository = thisRepository;
    }
    
    /// <summary>
    /// 查询游戏说明信息
    /// </summary>
    /// <returns></returns>
    public async Task<string> GetSummary()
    {
        var model =await _thisRepository.GetFirstAsync(m=>m.Status);
        return model.Summary;
    }

    /// <summary>
    /// 接受游戏完成后需要计算获得积分值
    /// </summary>
    /// <returns></returns>
    public async Task<int> AddSuccess(AppGameSuccessParam param)
    {
        var model =await _thisRepository.GetFirstAsync(m=>m.Status);
        
        var userRepository = _thisRepository.ChangeRepository<SugarRepository<Member>>();
        var userModel = await userRepository.GetByIdAsync(param.Uid);
        
        //计算所得积分
        var rule = model.ConfigRule[param.Level-1];
        var point = 0;
        foreach (var item in rule.Rule)
        {
            if (param.Step < item.Min || param.Step >= item.Max) continue;
            point = item.Point;
            break;
        }

        userModel.Point += point;
        await userRepository.UpdateAsync(userModel);
        //增加获得积分记录
        await _logService.AddAsync(new ShopPointsLogDto()
        {
            UserId = param.Uid,
            Flag = 1,
            GetType = ShopPointLogEnum.Game,
            Point = point
        });
        return point;
    }
}